import {Component, ElementRef} from '@angular/core';
import {IonicPage, NavController, ToastController, NavParams} from 'ionic-angular';
import {LoadingApi} from '../../api/loading/LoadingApi';
import { BarcodeScanner } from '@ionic-native/barcode-scanner';


@IonicPage()
@Component({
  selector: 'page-load-materiel',
  templateUrl: 'load-materiel.html'
})
export class LoadMateriel {

  private projectId: string;
  private planId: string;
  private planPositionId: string;
  private containerId: string;
  private qrcode: string;
  private materiel: object;
  private takePhoto: string;
  private param: any;
  private disTag: boolean = false;
  private LeaveTag: boolean = true;

  constructor(private barcodeScanner:BarcodeScanner,private params: NavParams, private ele: ElementRef, public navCtrl: NavController, private toastCtrl: ToastController, private loadingApi: LoadingApi) {
    this.materiel = {
      materielName: "",
      materielCode: "",
      materielTypeName: "",
      materielTypeCode: "",
      materielLength: "",
      materielWidth: "",
      materielHeight: "",
      materielEmptyWeight: "",
      materielGrossWeight: "",

      containerId: "",
      containerCode: "",
      containerType: "",
      containerName: "",
      containerLength: "",
      containerWidth: "",
      containerHeight: "",
      containerDeadWeight: "",
      containerBearWeight: "",
      containerPermission: "",

      materielCount: "",
      extraWeight:"",
      extraVolume: "",

    };
    this.projectId = params.get("projectId");
    this.planId = params.get("planId");
    this.planPositionId = params.get("planPositionId");
    this.containerId = params.get("containerId");
  }

  toPhoto(){
    this.navCtrl.push("ForcedPhoto",{
      "qrcode": this.qrcode,
      "planPositionId": this.planPositionId,
      "bizTag": false
    });
  }

  //获取焦点
  //点击开始扫描按钮
  scan() {
   // this.ele.nativeElement.querySelector("#scanmateriel").focus();
    this.ele.nativeElement.querySelector("#scanmaterielbtn").click();
  }

  //输入框值改变事件
  infoChange(val) {
    this.param = {
      "qrcode": val,
      "projectId": this.projectId,
      "planId": this.planId,
      "planPositionId": this.planPositionId,
      "containerId": this.containerId
    };
    this.scanMateriel(this.param);
  }

  toScan() {
    this.barcodeScanner.scan({showTorchButton:true,resultDisplayDuration:0}).then((data) => {
      if (data.text.length!=0){
        this.qrcode=data.text;
        this.param = {
          "qrcode": data.text,
          "projectId": this.projectId,
          "planId": this.planId,
          "planPositionId": this.planPositionId,
          "containerId": this.containerId
        };
        this.scanMateriel(this.param);
      }else{
        this.fixshowMsg("请扫描正确的二维码!");
      }
    }, (err)=> {
      this.showMsg("扫描出现异常" + err);
    });
  }

  scanMateriel(param: any) {
    //请求后台接口
    const http$ = this.loadingApi.scanMateriel(param);
    http$.subscribe((val: any) => {
      let scanResult = JSON.parse(val);
      if (scanResult.state) {
        // this.ele.nativeElement.querySelector("#scanmaterielbtn").click();
        this.materiel = scanResult.data;
        this.takePhoto=scanResult.data.takePhoto;
        for (var i = 0; i < this.ele.nativeElement.querySelectorAll(".hides").length; i++) {
          this.ele.nativeElement.querySelectorAll(".hides")[i].removeAttribute("hidden");
        }
        this.disTag = false;
        //此处判断强制拍照提示
        if(this.takePhoto=="0"){
          this.showMsg("货物属性为强制拍照,请拍照上传!");
          this.navCtrl.push("ForcedPhoto",{
            "qrcode": this.qrcode,
            "planPositionId": this.planPositionId
          });
        }
      } else {
        this.showMsg(scanResult.msg);
        //this.ele.nativeElement.querySelector("#scanmaterielbtn").click();
        //this.ele.nativeElement.querySelector("#scanmateriel").focus();
      }
    });
  }

  //点击打尺
  ruleMateriel() {
    //跳到打尺页面
    this.navCtrl.push("RuleRecord",{
      "qrcode":this.qrcode,
      "projectId": this.projectId,
      "planId": this.planId,
      "planPositionId": this.planPositionId,
      "materiel":this.materiel
    });
  }

  //点击取消返回
  goBack() {
    //this.ele.nativeElement.querySelector("[ng-reflect-klass='back-button']").click();
    this.navCtrl.pop();
  }

  showMsg(msg: string) {
    var toast = this.toastCtrl.create({
      message: msg,
      duration: 3000,
      position: 'bottom'
    });
    toast.present();
  }

  fixshowMsg(msg: string) {
    this.LeaveTag = false;
    var toast = this.toastCtrl.create({
      message: msg,
      duration: 3000,
      position: 'bottom'
    });
    toast.present();
    setTimeout(() => this.LeaveTag = true, 1000);
  }
}
